﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

using Newtonsoft.Json.Linq;

namespace KorbeRTR.MES
{
    public class SetInfoHelper
    {
        public const string Prefix = "HJ-KP-";

        [SetInfo(Prefix + "0001", "热水洗温度", "℃", "热水洗温度")]
        public double RS热水洗温度;

        [SetInfo(Prefix + "0002", "除油槽温度", "℃", "除油槽温度")]
        public double CY除油槽温度;

        /// <summary>
        /// 获取Json
        /// </summary>
        public JArray ToJson()
        {
            var array = new JArray();

            var fields = typeof(SetInfoHelper).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<SetInfoAttribute>();
                if (attr == null)
                {
                    continue;
                }

                var obj = new JObject
                {
                    ["ID"] = attr.ID,
                    ["Name"] = attr.Name,
                    ["Uint"] = attr.Uint,
                };

                switch(iter.GetValue(this))
                {
                    case bool tmp:
                        obj["NowValue"] = tmp;
                        break;

                    case int tmp:
                        obj["NowValue"] = tmp;
                        break;

                    case double tmp:
                        obj["NowValue"] = tmp;
                        break;

                    case string tmp:
                        obj["NowValue"] = tmp;
                        break;
                }

                array.Add(obj);
            }

            return array;
        }

        public void FromJson(JArray array)
        {
            var fields = typeof(SetInfoHelper).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<SetInfoAttribute>();
                if (attr == null)
                {
                    continue;
                }
            }
        }

        public void FromJson(JObject obj)
        {
            string id = obj["ID"].Value<string>();

            var fields = typeof(SetInfoHelper).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<SetInfoAttribute>();
                if (attr == null)
                {
                    continue;
                }

                if (id != attr.ID)
                {
                    continue;
                }

                iter.SetValue(this, Convert.ChangeType(obj["NowValue"], iter.FieldType));
            }
        }

        /// <summary>
        /// 获取注册表
        /// </summary>
        public static JArray GetRegistry()
        {
            var array = new JArray();

            var fields = typeof(SetInfoHelper).GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
            foreach (var iter in fields)
            {
                var attr = iter.GetCustomAttribute<SetInfoAttribute>();
                if (attr == null)
                {
                    continue;
                }

                var obj = new JObject
                {
                    ["ID"] = attr.ID,
                    ["Name"] = attr.Name,
                    ["Uint"] = attr.Uint,
                    ["Describe"] = attr.Describe
                };

                array.Add(obj);
            }

            return array;
        }
    }

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public class SetInfoAttribute : Attribute
    {
        public SetInfoAttribute(string ID, string Name, string Unit = "", string Describe = "")
        {
            this.ID = ID;
            this.Name = Name;
            this.Uint = Unit;
            this.Describe = Describe;
        }

        public string ID;

        public string Name;

        public string Uint;

        public string Describe;
    }
}
